﻿using AbpVnext_Module.Dto;
using AbpVnext_Module.Iservice;
using AbpVnext_Module.Model;
using AbpVnext_Module.Repository;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace AbpVnext_Module.Service
{
    [Dependency(ServiceLifetime.Transient)]
    public class PermissionService : CrudAppService<
                                      PermissionProduct,
                                      PermissionDto,
                                      PermissionPageResultDto,  //获取列表返回参数dto
                                      int, //主键
                                      PermissionPageDto, //查询入参
                                      PermissionCreateDto, //创建入参dto
                                      PermissionUpdateDto>  //更新入参dto
                                     , IPermissionService
    {
        public PermissionService(IPermissionRepository permissionRepository) : base(permissionRepository)
        {

        }

        //权限定义管理者
        public PermissionDefinitionManager permissionDefinitionManager { get; set; }

        /// <summary>
        /// 属性医药
        /// </summary>
        public IPermissionRepository permissionRepository { get; set; }

        //public IOrderService orderService { get; set; }

        public IObjectMapper _ObjectMapper { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<PermissionPageResultDto>> GetListAsync(PermissionPageDto input)
        {
            return await base.GetListAsync(input);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override async Task<PermissionDto> GetAsync(int id)
        {
            //return base.GetAsync(id);
            return await base.GetAsync(id);
        }

        /// <summary>
        /// 自定义方法
        /// </summary>
        public void CustomeLog()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[LogEnable]   //动态代理 拦截器(aop)
        //[AllowAnonymous]   //域名匿名访问,过滤鉴权拦截
        //[UnitOfWork]
        public override async Task<PermissionDto> CreateAsync(PermissionCreateDto input)
        {
            //return await base.CreateAsync(input);
            //将权限定义存储到数据库中
            IReadOnlyList<PermissionGroupDefinition> groupDefinitions = await permissionDefinitionManager.GetGroupsAsync();
            foreach (var item in groupDefinitions)
            {
                IReadOnlyList<PermissionDefinition> permissionDefinitions = item.Permissions;
                foreach (var it in permissionDefinitions)
                {
                    PermissionProduct permissionProduct = new PermissionProduct();
                    permissionProduct.PermissionName = it.Name;
                    permissionProduct.PermissionGroup = item.Name;
                    //添加数据
                    await Repository.InsertAsync(permissionProduct);
                }
            }
            return new PermissionDto();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PermissionDto> UpdateAsync(int id, PermissionUpdateDto input)
        {
            return await base.UpdateAsync(id, input);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override async Task DeleteAsync(int id)
        {
            await Repository.DeleteAsync(id);
        }

        public Task<string> CreatePerMange()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="permisionName"></param>
        /// <returns></returns>
        public async Task<string> GrantPermissonForuser(string userid, string permisionName)
        {
            return "yy";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleName"></param>
        /// <param name="permisionName"></param>
        /// <returns></returns>
        public async Task<string> GrantPermissonForRole(string RoleName, List<string> permisionName)
        {
            return "xx";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="RoleName"></param>
        /// <param name="permisionName"></param>
        /// <returns></returns>
        public async Task<string> GrantPermissonForEmail(string RoleName, List<string> permisionName)
        {
            return "ww";
        }
    }
}
